package com.iptv.grape.ui.control.ad;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.Channel;
import com.iptv.grape.domain.ChannelBill;
import com.iptv.grape.domain.IListViewShowItem;
import com.iptv.grape.domain.ListViewStyle;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.view.ListViewVScroll;

import java.util.List;

public class ListViewUI {
    // ListView控件本身
    private ListViewVScroll listViewVScroll;
    // ListView控件焦点ImageView
    private ImageView imageView;
    // 包括ListView的容器
    private RelativeLayout relativeLayout;
    // 序号
    private int index;
    private ListViewUIList mian;

    public ListViewUIList getMian() {
        return mian;
    }

    public void setMian(ListViewUIList mian) {
        this.mian = mian;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }



    public RelativeLayout getRelativeLayout() {
        return relativeLayout;
    }

    // 最后一次焦点下标
    private int lastFocusIndex = 0;
    // 每个元素的高度
    private int listViewItemHeight = 55;
    // 适配器
    private ListViewAdapter adapter;
    // 下级ListViewUI
    private ListViewUI nextListViewUI;

    private ListViewStyle curStyle;

    public ListViewStyle getCurStyle() {
        return curStyle;
    }

    public ListViewUI getNextListViewUI() {
        return nextListViewUI;
    }

    public boolean hasNextListViewUI(){
        return nextListViewUI != null;
    }

    public void setNextListViewUI(ListViewUI nextListViewUI) {
        this.nextListViewUI = nextListViewUI;
    }

    public ListViewUI(ListViewVScroll listViewVScroll, ImageView imageView, RelativeLayout relativeLayout,
                      int lastFocusIndex, int listViewItemHeight, ListViewAdapter adapter
                      ) {
        this.listViewVScroll = listViewVScroll;
        this.relativeLayout = relativeLayout;
        this.imageView = imageView;
        this.lastFocusIndex = lastFocusIndex;
        this.listViewItemHeight = listViewItemHeight;
        this.adapter = adapter;

        this.listViewVScroll.setMaxItemsInOneScreen(this.adapter.getMaxItemsInOneScreen());
        this.listViewVScroll.setItemHeight(this.listViewItemHeight);
        this.listViewVScroll.setAdapter(adapter);

        RelativeLayout.LayoutParams rLayoutParamsProgram = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (this.adapter.getMaxItemsInOneScreen()) * this.listViewItemHeight);
        this.listViewVScroll.setLayoutParams(rLayoutParamsProgram);
        rLayoutParamsProgram.addRule(RelativeLayout.CENTER_IN_PARENT);
        ListViewListener lvListener = new ListViewListener(this);
        this.listViewVScroll.setOnFocusChangeListener(lvListener);
        this.listViewVScroll.setIListViewVScroll(lvListener);

        this.listViewVScroll.setInitFocus(this.lastFocusIndex);
        this.listViewVScroll.setSelection(this.lastFocusIndex);
        this.listViewVScroll.setFocusable(true);
    }

//
//    // 刷新数据
//    // 日 比较坑 这个listViewVScroll控件做的不好  日他哦
//    public void refresh(List<IListViewShowItem> dataList){
//        //adapter = adapter.clone();
//        ListViewStyle style = dataList.get(0).getListViewStyle();
//        this.curStyle = style;
//        adapter.setDataList(dataList,style);
//
////        if(dataList.size() >= style.getMaxItemsInOneScreen()){
////            this.listViewVScroll.setMaxItemsInOneScreen(style.getMaxItemsInOneScreen());
////        }
////        else {
////            this.listViewVScroll.setMaxItemsInOneScreen(dataList.size());
////        }
//        this.listViewVScroll.setMaxItemsInOneScreen(style.getMaxItemsInOneScreen());
//
//        this.listViewVScroll.setItemHeight(style.getImageHeigth());
//
//        this.listViewVScroll.setAdapter(adapter);
//        RelativeLayout.LayoutParams rLayoutParamsProgram = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//                (style.getMaxItemsInOneScreen()) * style.getImageHeigth());
//        rLayoutParamsProgram.addRule(RelativeLayout.CENTER_IN_PARENT);
//
//        ViewGroup.LayoutParams layoutParams = this.relativeLayout.getLayoutParams();
//        layoutParams.width = style.getWidth();
//        ViewGroup.LayoutParams imageLayoutParams =  this.imageView.getLayoutParams();
//        imageLayoutParams.height = style.getImageHeigth();
//        this.relativeLayout.setLayoutParams(layoutParams);
//        this.imageView.setLayoutParams(imageLayoutParams);
//        if(style.getBackgroundColor() != null) {
//            this.imageView.setBackgroundColor(Color.parseColor(style.getBackgroundColor()));
//        }
//        this.listViewVScroll.setLayoutParams(rLayoutParamsProgram);
//
//
//
//        //ListViewListener lvListener = new ListViewListener(this);
//        //this.listViewVScroll.setOnFocusChangeListener(lvListener);
//        //this.listViewVScroll.setIListViewVScroll(lvListener);
//
//
//        this.listViewVScroll.setInitFocus(style.getInitFocus());
////        if(dataList.size() <= style.getInitFocus()) {
////            this.listViewVScroll.setInitFocus(0);
////        }
////        else {
////            this.listViewVScroll.setInitFocus(style.getInitFocus());
////        }
//        if(dataList.size() <= style.getSelection()) {
//            this.listViewVScroll.setSelection(0);
//        }
//        else {
//            this.listViewVScroll.setSelection(style.getSelection());
//        }
//        if(style.getDataType() == "ChannelBill"){
////            // 找到正在 直播的节目单
////            for(int index = 0; index < dataList.size(); index++){
////                ChannelBill channelBill = (ChannelBill)dataList.get(index);
////                if(channelBill.getBillType().equals("now")){
////                    this.listViewVScroll.setSelection(index);
////                    break;
////                }
////            }
////
////
////        }
//
//        this.listViewVScroll.setFocusable(true);
////        this.listViewVScroll.post(new Runnable() {
////            @Override
////           public void run() {
////                adapter.notifyDataSetChanged();
////            }
////        });
//
//
//
//    }


    // 刷新数据
    // 日 比较坑 这个listViewVScroll控件做的不好  日他哦
    public void refresh(List<IListViewShowItem> dataList){
        ListViewStyle style = dataList.get(0).getListViewStyle();
        this.curStyle = style;
        adapter.setDataList(dataList,style);
        this.listViewVScroll.setMaxItemsInOneScreen(style.getMaxItemsInOneScreen());

        this.listViewVScroll.setItemHeight(style.getImageHeigth());

        this.listViewVScroll.setAdapter(adapter);
        RelativeLayout.LayoutParams rLayoutParamsProgram = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (style.getMaxItemsInOneScreen()) * style.getImageHeigth());
        rLayoutParamsProgram.addRule(RelativeLayout.CENTER_IN_PARENT);

        ViewGroup.LayoutParams layoutParams = this.relativeLayout.getLayoutParams();
        layoutParams.width = style.getWidth();
        ViewGroup.LayoutParams imageLayoutParams =  this.imageView.getLayoutParams();
        imageLayoutParams.height = style.getImageHeigth();
        this.relativeLayout.setLayoutParams(layoutParams);
        this.imageView.setLayoutParams(imageLayoutParams);
        if(style.getBackgroundColor() != null) {
            this.imageView.setBackgroundColor(Color.parseColor(style.getBackgroundColor()));
        }
        this.listViewVScroll.setLayoutParams(rLayoutParamsProgram);

        this.listViewVScroll.setInitFocus(style.getInitFocus());
        if(dataList.size() <= style.getSelection()) {
            this.listViewVScroll.setSelection(0);
        }else {
            this.listViewVScroll.setSelection(style.getSelection());
        }
        AppLog.getCurLog().e("Type>>>"+style.getDataType());

        if( "Channel".equalsIgnoreCase(style.getDataType())){
            // 找到正在 直播的节目单
            for(int index = 0; index < dataList.size(); index++){
                Channel channel = (Channel)dataList.get(index);
                String channelID= GrapeContext.curGrapeContext().getChannelDLinkList().getChannelHis().getCurPlayChannel().getChannelID();
                AppLog.getCurLog().e("channelID>>>"+channel.getKeyID(),">>>",channelID);
                if(channel.getKeyID().equalsIgnoreCase(channelID)){
                    this.listViewVScroll.setSelection(index);
                    break;
                }
            }


        }

        if(style.getDataType() == "ChannelBill"){
            // 找到正在 直播的节目单
            for(int index = 0; index < dataList.size(); index++){
                ChannelBill channelBill = (ChannelBill)dataList.get(index);
                if(channelBill.getBillType().equals("now")){
                    this.listViewVScroll.setSelection(index);
                    break;
                }
            }


        }
        this.listViewVScroll.setFocusable(true);


    }



    public void show(){
        this.relativeLayout.setVisibility(View.VISIBLE);
        this.listViewVScroll.setVisibility(View.VISIBLE);
    }

    public void clear(){
        this.getAdapter().clear();
    }
    public void hide(){

        this.relativeLayout.setVisibility(View.GONE);
        this.listViewVScroll.setVisibility(View.GONE);
    }

    public void requestFocus(){
        this.listViewVScroll.requestFocus();
    }


    public ListViewVScroll getListViewVScroll() {
        return listViewVScroll;
    }

    public ImageView getImageView() {
        return imageView;
    }

    public int getLastFocusIndex() {
        return lastFocusIndex;
    }

    public int getListViewItemHeight() {
        return listViewItemHeight;
    }

    public void setLastFocusIndex(int lastFocusIndex) {
        this.lastFocusIndex = lastFocusIndex;
    }

    public ListViewAdapter getAdapter() {
        return adapter;
    }
}
